package com.syz.mall.controller;

import com.syz.mall.pojo.Product;
import com.syz.mall.pojo.query.ProductQuery;
import com.syz.mall.service.ICategoryService;
import com.syz.mall.service.IProductService;
import com.syz.mall.util.LayUITableResult;
import com.syz.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.rmi.StubNotFoundException;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @Autowired
    private ICategoryService categoryService;
    @RequestMapping("/getProductListPage")
    public String getProductListPage() {
        return "/ProductManage/Product/product_list";
    }
    @RequestMapping("/getProductAddPage")
    public String getProductAddPage() {
        return "/ProductManage/Product/product_add";
    }
    @RequestMapping("/getProductUpdatePage")
    public String getProductUpdatePage(Integer id, Model model){
        Product product=productService.selectById(id);
        model.addAttribute("product",product);
        model.addAttribute("topCategory",categoryService.selectTopById(product.getCategoryId()));
        model.addAttribute("curCategory",categoryService.selectById(product.getCategoryId()));
        return "/ProductManage/Product/product_update";
    }

    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(ProductQuery productQuery) {
        System.out.println("ProductController.selectByPage");
        return productService.selectByPage(productQuery);
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        boolean isSuccess = productService.deleteAll(ids);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        boolean isSuccess = productService.deleteById(id);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(Product product) {
        if(product.getMainImage().equals("")){
            product.setMainImage(null);
        }
        boolean isSuccess = productService.add(product);
        return isSuccess ? Result.ok("添加成功") : Result.error("添加失败");
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(Product product){
        boolean isSuccess = productService.update(product);
        return isSuccess ? Result.ok("更新成功") : Result.error("更新失败");
    }

    @RequestMapping("/changeStatus")
    @ResponseBody
    public Result update(Integer id,Integer status){
        boolean isSuccess = productService.changeStatus(id,status);
        return isSuccess ? Result.ok("更新成功") : Result.error("更新失败");
    }
}
